package com.prj.ufdm.bsplatform.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.prj.ufdm.auth.bpo._impl.SysUserBPOImpl;
import com.prj.ufdm.auth.model.SysUser;
import com.prj.ufdm.bsplatform.bpo.BsplatformBPO;
import com.prj.ufdm.bsplatform.enums.BspfMethodEnum;
import com.prj.ufdm.bsplatform.model.BspfParam;
import com.prj.ufdm.core.util.UfdmRequestUtil;

/**
 * DESC: 标准化平台登录 <br>
 * @author 胡义振
 * @date 2020年4月29日
 */
@RequestMapping(value = "/bsplatform")
@Controller
public class BspUserLoginController {

	@Value("${client.server.url:}")
	private String clientServerUrl;
	// 登录密码
	private final String defaultLoginPass = "123456";
	@Autowired
	SysUserBPOImpl sysUserBPO;
	@Autowired
	BsplatformBPO bsplatformBPO;

	/**
	 * DESC: 返回菜单树方式登录
	 * * 调用三次
	 * * 第一次 获取随机数 getVerifyCode
	 * * 第二次 登录第三方应用系统loginApp
	 * * 第三次 获取第三方应用系统菜单树getTreeNode
	 * @auther 胡义振
	 * @date 2020年4月30日
	 * @param request
	 * @param response
	 * @throws Exception 
	 * @return
	 */
	@RequestMapping(value = "/login/menutree")
	@ResponseBody
	public void loginForMenuTree(HttpServletRequest request,HttpServletResponse response) throws Exception {
		
		System.out.println("\n 返回菜单树方式登录："+ UfdmRequestUtil.getAllParam(request));
		BspfParam bspfParam = bsplatformBPO.getLoginParam(request);
        // 判断是否获取验证码接口
        if(BspfMethodEnum.GET_VERIFY_CODE.getKey().equals(bspfParam.getMethod())){
        	bsplatformBPO.responseVerifyCode(request, response);
        	return;
   	    }
        // 登录是否为登录接口
        else if(BspfMethodEnum.GET_LOGIN_APP.getKey().equals(bspfParam.getMethod())){
        	// 校验验证码
        	if(!bspfParam.getParam().getScode().equals(bspfParam.getSessionVerifycode())) {
        		bsplatformBPO.responseVerifyCodeErr(request, response);
        		return;
        	}
        	else {
	        	bsplatformBPO.responseOk(request, response);
	        	return;
        	}
    	}
        // 获取菜单树
        else if(BspfMethodEnum.GET_TREE_NODE.getKey().equals(bspfParam.getMethod())){
        	bsplatformBPO.responseTreeNode(request, response, "1");
        	return;
        }
        
    }
	
	/**
	 * Desc: 首页集成方式 登录
	 * # 调用两次
	 * # 第一次 获取随机数 getVerifyCode
	 * # 第二次 登录第三方应用系统 loginApp
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/login/redirect")
	@ResponseBody
	public void login(HttpServletRequest request,HttpServletResponse response) throws Exception {
		
		System.out.println("\n 首页集成方式登录："+ UfdmRequestUtil.getAllParam(request));

		BspfParam bspfParam = bsplatformBPO.getLoginParam(request);
        // 判断是否获取验证码接口
        if(BspfMethodEnum.GET_VERIFY_CODE.getKey().equals(bspfParam.getMethod())){
        	bsplatformBPO.responseVerifyCode(request, response);
        	return;
   	    }
        // 登录是否为登录接口
        else if(BspfMethodEnum.GET_LOGIN_APP.getKey().equals(bspfParam.getMethod())){
        	// 校验验证码
        	if(!bspfParam.getParam().getScode().equals(bspfParam.getSessionVerifycode())) {
        		bsplatformBPO.responseVerifyCodeErr(request, response);
        		return;
        	}
        	else {
				SysUser sysUser = sysUserBPO.doGetSysUserByUsername(bspfParam.getParam().getLoginname());
				if(null != sysUser) {
				    //response.sendRedirect(clientServerUrl+"/#/login?loginType=third&username="+bspfParam.getParam().getLoginname()+"&password="+defaultLoginPass);
				    response.sendRedirect("/web/index.html");
				    return;
				}else {
					response.sendRedirect(clientServerUrl);
					return;
				}
        	}
    	}
    }
	
}
